<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="物料名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入物料名称"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物料编码" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入物料编码"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物料型号" prop="model">
        <el-input
          v-model="queryParams.model"
          placeholder="请输入物料型号"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['asset:material:set']"
          >新增</el-button
        >
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="materialList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column label="序号" width="60" align="center" type="index" />
      <el-table-column label="物料名称" align="center" prop="name" :show-overflow-tooltip="true" />
      <el-table-column label="物料编码" align="center" prop="code" :show-overflow-tooltip="true" />
      <el-table-column label="物料型号" align="center" prop="model" :show-overflow-tooltip="true" />
      <el-table-column label="单位" align="center" prop="unit" :show-overflow-tooltip="true" />
      <el-table-column label="厂家" align="center" prop="factory" :show-overflow-tooltip="true" />
      <el-table-column label="存放地点" align="center" prop="address" :show-overflow-tooltip="true" />
      <el-table-column label="价格" align="center" prop="price" :show-overflow-tooltip="true">
        <template #default="scope">
          <span>￥{{ scope.row.price }}</span>
        </template>
      </el-table-column>
      <!-- 物料总数 -->
      <el-table-column label="物料总数" align="center" prop="total" :show-overflow-tooltip="true" />
      <el-table-column label="出库数量" align="center" prop="outbound" :show-overflow-tooltip="true" />
      <el-table-column label="当前库存" align="center" prop="currentTotal" :show-overflow-tooltip="true" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="180"
        :show-overflow-tooltip="true"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        width="200"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['asset:material:set']"
            >修改</el-button
          >
          <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['asset:material:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改物料对话框 -->
    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <el-form
        ref="materialRef"
        :model="form"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="物料名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入物料名称" />
        </el-form-item>
        <el-form-item label="物料编码" prop="code">
          <el-input v-model="form.code" placeholder="请输入物料编码" />
        </el-form-item>
        <el-form-item label="物料型号" prop="model">
          <el-input v-model="form.model" placeholder="请输入物料型号" />
        </el-form-item>
        <el-form-item label="单位" prop="unit">
          <el-input v-model="form.unit" placeholder="请输入物料单位" />
        </el-form-item>
        <el-form-item label="厂家" prop="factory">
          <el-input v-model="form.factory" placeholder="请输入厂家信息" />
        </el-form-item>
        <el-form-item label="存放地点" prop="address">
          <el-input v-model="form.address" placeholder="请输入存放地点" />
        </el-form-item>
        <el-form-item label="价格（￥）" prop="price">
          <el-input-number
            v-model="form.price"
            :precision="2"
            :min="0"
            :step="0.1"
            style="width: 100%"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm" :loading="submitLoading">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Material">
import {
  listMaterial,
  addMaterial,
  delMaterial,
} from "@/api/asset/material";
const { proxy } = getCurrentInstance();

const materialList = ref([]);
const open = ref(false);
const loading = ref(false);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const submitLoading = ref(false);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: undefined,
    code: undefined,
    model: undefined,
  },
  rules: {
    name: [{ required: true, message: "物料名称不能为空", trigger: "blur" }],
    code: [{ required: true, message: "物料编码不能为空", trigger: "blur" }],
    unit: [{ required: true, message: "物料单位不能为空", trigger: "blur" }],
  },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询物料列表 */
function getList() {
  loading.value = true;
  listMaterial(queryParams.value).then((response) => {
    materialList.value = response.data.rows;
    total.value = response.data.total;
    loading.value = false;
  });
}

/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}

/** 表单重置 */
function reset() {
  form.value = {
    materialId: '',
    name: undefined,
    code: undefined,
    model: undefined,
    unit: undefined,
    factory: undefined,
    address: undefined,
    price: 0,
    // outbound:""
  };
  proxy.resetForm("materialRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  Object.keys(queryParams.value).forEach((key) => {
    if (key !== "pageNum" && key !== "pageSize") {
      queryParams.value[key] = undefined;
    }
  });
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.materialId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加物料";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  form.value = Object.assign({}, form.value, row);
  open.value = true;
  title.value = "修改物料";
  //   const materialId = row.materialId || ids.value;
  //   getMaterial(materialId).then((response) => {
  //     form.value = response.data;
  //     open.value = true;
  //     title.value = "修改物料";
  //   });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["materialRef"].validate((valid) => {
    if (valid) {
      submitLoading.value = true;
      if (form.value.materialId != '') {
        addMaterial(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        }).catch(() => {
          
        }).finally(() => {
          submitLoading.value = false;
        });
      } else {
        form.value.materialId = '0'
        addMaterial(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false; 
          getList();
        }).catch(() => {
          
        }).finally(() => {
          submitLoading.value = false;
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const materialIds = row.materialId || ids.value;
  proxy.$modal
    .confirm('是否确认删除物料编号为"' + materialIds + '"的数据项？')
    .then(function () {
      return delMaterial(materialIds);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

// 页面初始化
onMounted(() => {
  getList();
});

// 页面激活时
onActivated(() => {
  console.log('888')
  getList();
});
</script>
